﻿using System;
using System.Xml;

using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Description;
using System.Security.Cryptography.X509Certificates;
using Microsoft.ServiceModel.Web;


namespace USTF.Media.MediaService.UmbracoDefault
{
    public class UmbracoDefaultImageMediaServiceFactory : ServiceHostFactory
    {
        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
            
            ServiceHost host = new ServiceHost(typeof(UmbracoDefaultImageMediaService),baseAddresses);

            var binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
            binding.MaxBufferPoolSize = 2147483647;
            binding.MaxReceivedMessageSize = 2147483647;
            binding.ReaderQuotas.MaxStringContentLength = 2147483647;
            binding.ReaderQuotas.MaxArrayLength = 2147483647;
            binding.ReaderQuotas.MaxBytesPerRead = 2147483647;
            binding.ReaderQuotas.MaxDepth = 2147483647;
            binding.ReaderQuotas.MaxNameTableCharCount = 2147483647;            
            
           // binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
            
            //add authentication behavior
           // var securBehav = host.Description.Behaviors.Find<ServiceCredentials>();
          //  securBehav.UserNameAuthentication.UserNamePasswordValidationMode = System.ServiceModel.Security.UserNamePasswordValidationMode.Custom;
           // securBehav.UserNameAuthentication.CustomUserNamePasswordValidator = new UmbracoMediaServiceAuthenticator();
            //securBehav.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindBySubjectName, "localhost");
          //  host.Description.Behaviors.Add(securBehav);

            

            //add exception details
            var debugBehav = host.Description.Behaviors.Find<ServiceDebugBehavior>();
            if (debugBehav == null)
            {
                debugBehav = new ServiceDebugBehavior();
                host.Description.Behaviors.Add(debugBehav);
            }
            debugBehav.IncludeExceptionDetailInFaults = true;

            //enable http get for the metadata
            var metaBehav = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
            if (metaBehav == null)
            {
                metaBehav = new ServiceMetadataBehavior();
                host.Description.Behaviors.Add(metaBehav);
            }
            metaBehav.HttpGetEnabled = true;
            
            //set binding
            host.Description.Endpoints.Clear();
            host.AddServiceEndpoint(typeof(IUSTFMediaService), binding, String.Empty);
            host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "mex");

            //add behavior to show error details to silverlight clients
            host.Description.Endpoints.Find(typeof(IUSTFMediaService))
                    .Behaviors.Add(new SilverlightWCFFaultBehavior());
            
            //add authentication behavior
            host.Description.Endpoints.Find(typeof(IUSTFMediaService))
                .Behaviors.Add(new BasicAuthenticationBehavior());
    
            return host;
        }
    }
}
